﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AABB {

    private Vector3 pos;
    private float XL;
    private float YL;
    private float ZL;
    private float XL_half;
    private float YL_half;
    private float ZL_half;
    public float lv { get; set; }
    public GameObject go { get; set; }


    public AABB(GameObject go,Vector3 pos, float XL, float YL, float ZL, float lv)
    {
        this.go = go;
        this.pos = pos;
        this.XL = XL;
        this.YL = YL;
        this.ZL = ZL;
        this.XL_half = XL / 2;
        this.YL_half = YL / 2;
        this.ZL_half = ZL / 2;
        this.lv = lv;
    }

    public void UpdatePos(Vector3 pos)
    {
        this.pos = pos;
    }
    public Vector3 GetMinVec()
    {
        return new Vector3(this.pos.x - this.XL_half, this.pos.y - this.YL_half, this.pos.z - this.ZL_half);
    }

    public Vector3 GetMaxVec()
    {
        return new Vector3(this.pos.x + this.XL_half, this.pos.y + this.YL_half, this.pos.z + this.ZL_half);
    }

    public bool Check(AABB aabb)
    {
        Vector3 minVec1 = aabb.GetMinVec();
        Vector3 maxVec1 = aabb.GetMaxVec();
        Vector3 minVec2 = this.GetMinVec();
        Vector3 maxVec2 = this.GetMaxVec();
        //Debug.Log(Convert.ToString(minVec1.x) + " "+ Convert.ToString(minVec1.y) + " " + Convert.ToString(minVec1.z));
        //Debug.Log(Convert.ToString(maxVec1.x) + " "+ Convert.ToString(maxVec1.y) + " " + Convert.ToString(maxVec1.z));
        //Debug.Log(Convert.ToString(minVec2.x) + " "+ Convert.ToString(minVec2.y) + " " + Convert.ToString(minVec2.z));
        //Debug.Log(Convert.ToString(maxVec2.x) + " "+ Convert.ToString(maxVec2.y) + " " + Convert.ToString(maxVec2.z));
        if(minVec1.x > maxVec2.x) { return false; }
        if(minVec1.y > maxVec2.y) { return false; }
        if(minVec1.z > maxVec2.z) { return false; }

        if(minVec2.x > maxVec1.x) { return false; }
        if(minVec2.y > maxVec1.y) { return false; }
        if(minVec2.z > maxVec1.z) { return false; }
        return true;
    }

}
